package com.cccyy.codecache.initdata;

import com.cccyy.codecache.CityDao;
import com.cccyy.codecache.TownNameDto;
import org.jcodec.common.logging.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InitDataListener implements InitializingBean, ServletContextAware {

    @Resource
    private CityDao cityDao;
    public static Map<String, TownNameDto> codeMap = new HashMap<String, TownNameDto>();

    @Override
    public void afterPropertiesSet() throws Exception {
        Logger.info("这是一个重写的方法，interface InitializingBean");
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        List<TownNameDto> codeList = cityDao.selectCityNameAndCodeALL();
        for (TownNameDto code : codeList) {
            codeMap.put(code.getParentCode(), code);
        }
    }
}

